package ReSofting.it.GoldDirector.Helpers;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONStringer;

import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.widget.ArrayAdapter;

public  class WcfClient {

	
	public static String SERVICE_URI = "http://192.168.1.2:8096/Services/OrdersSvc.svc/";
	
	
	//
	
	
	// mylistAntipasti
	public static  JSONArray getItems(String pWcfMethod) {
	    try {
	 
	        // Send GET request to <service>/GetPlates
	        HttpGet request = new HttpGet(SERVICE_URI + pWcfMethod);
	        request.setHeader("Accept", "application/json");
	        request.setHeader("Content-type", "application/json");
	 
	        DefaultHttpClient httpClient = new DefaultHttpClient();
	        HttpResponse response = httpClient.execute(request);
	 
	        HttpEntity responseEntity = response.getEntity();
	         
	        // Read response data into buffer
	        char[] buffer = new char[(int)responseEntity.getContentLength()];
	        InputStream stream = responseEntity.getContent();
	        InputStreamReader reader = new InputStreamReader(stream);
	        reader.read(buffer);
	        stream.close();
	 
	        JSONArray items = new JSONArray(new String(buffer));
	 
	        return items;
	        

	         
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    
	    return null;
	}//fM
	
	
	
	public static void getAsync()
	{
		  new Thread(new Runnable() 
          {
		       public void run()
		       {}  
          }
   
   ).start();
          
		
		
	}//fM
	
	
	/*
	 * 
	 * 
	 
	 public void onLoadVehicleClick(View button) {
    try {
        // Send GET request to <service>/GetVehicle/<plate>
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(SERVICE_URI + "/GetVehicle/" + plateSpinner.getSelectedItem());
 
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");
 
        HttpResponse response = httpClient.execute(request);
 
        HttpEntity responseEntity = response.getEntity();
 
        // Read response data into buffer
        char[] buffer = new char[(int)responseEntity.getContentLength()];
        InputStream stream = responseEntity.getContent();
        InputStreamReader reader = new InputStreamReader(stream);
        reader.read(buffer);
        stream.close();
 
        JSONObject vehicle = new JSONObject(new String(buffer));
 
        // Populate text fields
        makeEdit.setText(vehicle.getString("make"));
        plateEdit.setText(vehicle.getString("plate"));
        modelEdit.setText(vehicle.getString("model"));
        yearEdit.setText(vehicle.getString("year"));
 
    } catch (Exception e) {
        e.printStackTrace();
    }
}
	 
	 	 
	public void onSaveVehicleClick(View button) {
		 
	    try {
	 
	        Editable make = makeEdit.getText();
	        Editable plate = plateEdit.getText();
	        Editable model = modelEdit.getText();
	        Editable year = yearEdit.getText();
	 
	        boolean isValid = true;
	 
	        // Data validation goes here
	 
	        if (isValid) {
	 
	            // POST request to <service>/SaveVehicle
	            HttpPost request = new HttpPost(SERVICE_URI + "/SaveVehicle");
	            request.setHeader("Accept", "application/json");
	            request.setHeader("Content-type", "application/json");
	 
	            // Build JSON string
	            JSONStringer vehicle = new JSONStringer()
	                .object()
	                    .key("vehicle")
	                        .object()
	                            .key("plate").value(plate)
	                            .key("make").value(make)
	                            .key("model").value(model)
	                            .key("year").value(Integer.parseInt(year.toString()))
	                        .endObject()
	                    .endObject();
	            StringEntity entity = new StringEntity(vehicle.toString());
	 
	            request.setEntity(entity);
	 
	            // Send request to WCF service
	            DefaultHttpClient httpClient = new DefaultHttpClient();
	            HttpResponse response = httpClient.execute(request);
	 
	            //Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
	             
	            // Reload plate numbers
	           // refreshVehicles();
	        }
	 
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	*/
	
	
	public static void Ping()
	{
		
	
	}
	
	
	
}//fC
